import 'package:flutter/material.dart';
import '../../Tools/ToolsLocalStorage.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '../settingsSub/PassWordPage.dart';
import '../../widgets/AppColors.dart';

class AccountPage extends StatefulWidget {
  const AccountPage({super.key});

  @override
  _AccountPageState createState() => _AccountPageState();
}

class _AccountPageState extends State<AccountPage> {
  late Future<Map<String, dynamic>> user; // 使用 late 声明
  static const array = ['手机号码', '更换密码', '个人头像'];
  @override
  void initState() {
    super.initState();
    user = LocalStorage.getJson('user'); // 在 initState 中初始化
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      appBar: AppBar(
        title: const Text('账号与安全'),
        backgroundColor: Colors.transparent,
        foregroundColor: AppColors.textPrimary,
        flexibleSpace: Container(
          decoration: const BoxDecoration(
            image: DecorationImage(
              image: AssetImage('assets/images/home/home_topbg.png'),
              fit: BoxFit.cover,
            ),
          ),
        ),
      ),
      body: Column(
        children: [
          Container(
            height: array.length * 60,
            margin: const EdgeInsets.all(15.0),
            decoration: BoxDecoration(
              color: AppColors.cardColor,
              borderRadius: BorderRadius.circular(10.0),
            ),
            child: ListView.builder(
              itemCount: array.length,
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              itemBuilder: (context, index) {
                return GestureDetector(
                  onTap: () {},
                  child: _buildListItem(
                      array[index], index, index == array.length - 1),
                );
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildListItem(String title, int index, bool isLast) {
    return Container(
      padding: const EdgeInsets.all(15.0),
      height: 60,
      decoration: BoxDecoration(
        border: Border(
          bottom: isLast
              ? BorderSide.none
              : BorderSide(color: AppColors.dividerColor),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween, // 使内容两端对齐
            children: [
              Row(
                children: [
                  Text(
                    title,
                    style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                        color: AppColors.textPrimary),
                  ),
                ],
              ),
              if (index == 0)
                FutureBuilder<Map<String, dynamic>>(
                  future: user,
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return Text(
                        snapshot.data!['phonenumber'] ?? '',
                        style: TextStyle(
                            fontSize: 15, color: AppColors.textSecondary),
                      );
                    }
                    return CircularProgressIndicator();
                  },
                ),
              if (index == 2)
                FutureBuilder<Map<String, dynamic>>(
                  future: user,
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return TDImage(
                        imgUrl: 'http://8.137.93.51:8080' +
                            snapshot.data!['avatar'],
                        type: TDImageType.circle,
                        width: 30,
                        height: 30,
                      );
                    }
                    return CircularProgressIndicator();
                  },
                ),
              if (index == 1)
                GestureDetector(
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const PassWordPage()),
                    );
                  },
                  child: Container(
                    color: AppColors.cardColor,
                    child: Row(
                      children: [
                        const SizedBox(width: 60),
                        const Align(
                          alignment: Alignment.center,
                          child: Image(
                              image:
                                  AssetImage('assets/images/setting/push.png')),
                        ),
                      ],
                    ),
                  ),
                ),
            ],
          ),
        ],
      ),
    );
  }
}
